var index_province = 0;
var index_city = 0;
var index_area = 0;
var area_name = "";
var cityid = "";
var loc;
var lunar_ids = [6,7,8,9,10];

function init(){
	loc = new Location();
	loc.fillProvince("province_list",0 ,index_province);
	$("#province_list li[class!='except']").bind("click", function(){set_city(this);});
	$("#province_list li[class!='except']").mouseover(function(){$(this).addClass("red")});
	$("#province_list li[class!='except']").mouseout(function(){$(this).removeClass("red")});
	$("a[rel='external']").attr("target","_blank");	// open new windows
	get_current_city_info();
}


function set_city(el){
	index_province = $(el).attr("id");
	cityid = "";
	$("#city_list").html("");
	$("#area_list").html("");
	loc.fillProvince("city_list","0,"+index_province ,index_city);
	$("#city_list li[class!='except']").bind("click", function(){set_area(this);});
	$("#city_list li[class!='except']").mouseover(function(){$(this).addClass("red")});
	$("#city_list li[class!='except']").mouseout(function(){$(this).removeClass("red")});
	$("#province_list li").each(function(index, element){$(this).removeClass("bold").removeClass("blue");});
	$(el).addClass("bold").addClass("blue");
	$("#url_list").fadeOut(1000);
}

function set_area(el){
	index_city = $(el).attr("id");
	$("#area_list").html("");
	cityid = "";
	loc.fillProvince("area_list","0,"+index_province+","+index_city ,index_city);
	$("#area_list li[class!='except']").bind("click", function(){set_url(this);});
	$("#area_list li[class!='except']").mouseover(function(){$(this).addClass("red")});
	$("#area_list li[class!='except']").mouseout(function(){$(this).removeClass("red")});
	$("#city_list li[class!='except']").each(function(index, element){$(this).removeClass("bold").removeClass("blue");});
	$(el).addClass("bold").addClass("blue");
	set_url(el);
}

function set_url(el){
	index_area = $(el).attr("id");
	area_name = $(el).html();
	cityid = "";
	$("#area_list li").each(function(index, element){$(this).removeClass("bold").removeClass("blue");});
	$(el).addClass("bold").addClass("blue");
	$("#url_list").fadeIn(1000);
	$("#result_url").html("");
	$("#result_content").html("");
	// 选择城市时直接显示iPhone天气地址
	search_city_code_local(0);
}

function search_city_code(i){
	if(area_name.length == 0) return;
	if(cityid!=""){get_url(i, cityid);return;}
	$("#result_url").html("<img src=\"/spbweatherinfo/images/loading.gif\" width=\"16\" height=\"16\">");
	$.ajax({
		type: "POST",
		url: "/weather/interface.php",
		data: "job=get_weathercn_code&cityname=" + escape(area_name),
		cache: false,
		dataType: "html",
		success:function(str){
			cityid = str;
			get_url(i,str);
		},
		error:function(XMLHttpRequest,Status,errorThrown){
			alert("XMLHttpRequest:" + XMLHttpRequest + "\r\nStatus:"+Status+"\r\nerrorThrown:"+errorThrown);
		}
	});
}

function search_city_code_local(i){
	if(area_name.length == 0) return;
	$("#result_url").html("<img src=\"/images/loading.gif\" width=\"16\" height=\"16\">");
	var H = Array("","市","县","区","镇","乡");
	//var city = area_name.length<=2 ? area_name : area_name.replace("市","").replace("县","").replace("区","");
	//city = city.length<=2 ? city : city.replace("州","");
	var city = $.inArray(area_name.substr(-1), H)>-1 ? area_name.substr(0, area_name.length-1) : v;
	var str = map.getKey(city);
	get_url(i,str);
}

function get_url(i,str){
	var p = $("#option_enable_lunar").find("option:selected").val();
	if($.inArray(parseInt(p), lunar_ids) != -1){	// 纯农历
		str = "";
		option = "n"+p;
	}else{
		if(str == null){
			$("#result_url").html("没有找到城市代码！");return;
		}else{
			// 设置内容
			option = p==1 ? "" : ","+p;
			i==0 ? $("#option_enable_lunar").show() : $("#option_enable_lunar").hide();
		}
	}
	
	if(i==0){
		$("#result_url").html("http://w.mdeve.com/" + str + option + ".ics");
		$("#result_content").html("iPhone日历天气，具体用法请看：<a href=\"http://mdeve.com/blog/377.html\" target=\"_blank\">http://mdeve.com/blog/377.html</a>");
	}else if(i==1){
		$("#result_url").html("http://w.mdeve.com/" + str + ".gics");
		$("#result_content").html("Google日历短信提醒，具体用法请看：<a href=\"http://mdeve.com/blog/412.html\" target=\"_blank\">http://mdeve.com/blog/412.html</a>");
	}
}

function get_current_city_info(){
	$.post("/weather/interface.php", {job:"get_current_city_info"}, function(data){
		$("#my_weather").html(data);
	});

}

// isNaN(s)==true 不是数字
function search_city(){
	var s = $("#s").val();
	var re = /[\u4e00-\u9fa5]/;
	if(s.length==0) return false;
	if(s.match(re)!=null){		// 为中文，直接搜索内容
		find_city(s);
	}else{	// 不为中文，ajax查询其中文名字
		$.ajax({
			type: "POST",
			url: "/weather/interface.php",
			data: "job=get_city_name&cityname=" + escape(s),
			cache: false,
			dataType: "html",
			success:function(str){
				if(str.length>0){
					find_city(str);
				}else{
					alert("没有找到地区：" + s);
				}
			},
			error:function(XMLHttpRequest,Status,errorThrown){
				alert("XMLHttpRequest:" + XMLHttpRequest + "\r\nStatus:"+Status+"\r\nerrorThrown:"+errorThrown);
			}
		}); 
	}
}

function find_city(s){
	var id_area = "";	// 当前级ID
	var id_city = "";	// 上级ID
	var H = Array("","市","县","区","镇","乡");
	// 如果没找到
	outerloop://命名外圈语句 
	if(id_area.length==0 || id_city.length==0){
		for(var i=0; i<H.length; i++){
			for(var k in loc.items){
				for(var k1 in loc.items[k]){
					if(s+H[i] == loc.items[k][k1]){	// 已找到
						id_area = k1;
						id_city = k;
						break outerloop;
					}
				}
			}
		}
	}
	
	if(id_area.length>0 && id_city.length>0){	// 已找到
		// 获取省份ID
		if(id_city.indexOf(",") == -1){
			alert("只能搜索城市！");
			return false;
		}
		ids = id_city.split(",");
		// 查找已显示的省份
		$("#province_list li").each(function(index, element){
			$(this).removeClass("bold").removeClass("blue");
			if(ids[1] == $(element).attr("id")){	// 已找到城市
				set_city(element);	// 设置城市
				if(ids[2] == undefined){	// 第3个参数为空，找的是城市
					$("#city_list li").each(function(index1, element1){
						if(id_area == $(element1).attr("id")){	// 已找到
							set_area(element1);
							set_url(element1);
						}
					});
				}else{
					$("#city_list li").each(function(index1, element1){
						if(ids[2] == $(element1).attr("id")){	// 已找到区县
							set_area(element1);
							$("#area_list li").each(function(index2, element2){
								if(id_area == $(element2).attr("id")){	// 已找到
									set_url(element2);
								}
							});
						}
					});
				}
			}
		});
	}
	
}
